package com.wgts.wgts.mvp.view.activity.timeaction;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.nestedscrollview.JudgeNestedScrollView;
import com.wgts.wgts.control.pagertitle.TimeActionPagerTitleView;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.control.viewpage.ComFragmentAdapter;
import com.wgts.wgts.control.viewpage.WrapContentHeightViewPager;
import com.wgts.wgts.mvp.view.fragment.TimeAction;
import com.wgts.wgts.test.TestViewPageFragment;
import com.wgts.wgts.tools.ScreenUtil;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.TriangularPagerIndicator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 限时活动界面
 */
public class TimeActionActivity extends BaseActivity {


    private static final String TAG = "TimeActionActivity";

    @BindView(R.id.title)
    View title;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.scrollView)
    JudgeNestedScrollView scrollView;
    @BindView(R.id.img_back_init)
    ImageView imgBackInit;
    @BindView(R.id.linear_suspension)
    LinearLayout linearSuspension;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_suspension_title)
    TextView tvSuspensionTitle;
    @BindView(R.id.relative_suspension)
    RelativeLayout relativeSuspension;
    @BindView(R.id.magic_indicator_suspension)
    MagicIndicator magicIndicatorSuspension;
    @BindView(R.id.linear_title)
    LinearLayout linearTitle;
    @BindView(R.id.view_page)
    WrapContentHeightViewPager viewPage;
    private CustomLineaLayoutManager customLineaLayoutManager;

    private float titleViewHeight = 0;


    private TimeAction[] mTitles =
            new TimeAction[]{
                    new TimeAction("08月10日", "正在抢购"),
                    new TimeAction("08月11日", "即将开抢"),
                    new TimeAction("08月12日", "即将开抢"),
                    new TimeAction("08月13日", "即将开抢"),
                    new TimeAction("08月14日", "即将开抢"),
                    new TimeAction("08月15日", "即将开抢"),
                    new TimeAction("08月16日", "即将开抢"),
                    new TimeAction("08月17日", "即将开抢"),
                    new TimeAction("08月18日", "即将开抢")};
    private List<TimeAction> mDataList = Arrays.asList(mTitles);


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        titleViewHeight = DensityUtil.px2dp(title.getHeight());
        Log.d(TAG, titleViewHeight + "");
    }

    @Override
    public void init() {

    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.time_action_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        ArrayList<Fragment> fragments = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            fragments.add(new TestViewPageFragment());
        }

        scrollView.post(new Runnable() {
            @Override
            public void run() {
                dealWithViewPager();
            }
        });

        tvSuspensionTitle.setText("限时抢购");

        viewPage.setAdapter(new ComFragmentAdapter(getSupportFragmentManager(), fragments));

        customLineaLayoutManager = new CustomLineaLayoutManager(this);
        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView nestedScrollView, int i, int i1, int i2, int i3) {
                scrollView.setNeedScroll(false);
                Log.d(TAG, "nestedScrollView:" + " i:" + DensityUtil.px2dp(i) + " i1:" + DensityUtil.px2dp(i1) + " i2:" + DensityUtil.px2dp(i2) + " i3:" + DensityUtil.px2dp(i3));
            }
        });
        initMagicIndicator();
        viewPage.setOffscreenPageLimit(10);
    }


    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(TimeActionActivity.this);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdjustMode(false);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                TimeActionPagerTitleView timeActionPagerTitleView = new TimeActionPagerTitleView(context);
                timeActionPagerTitleView.setTextActionStateText(mDataList.get(index).actionTime);
                timeActionPagerTitleView.setTextActionTimeText(mDataList.get(index).actionStatue);
                timeActionPagerTitleView.setNormalColor(ContextCompat.getColor(TimeActionActivity.this, R.color.nomal));
                timeActionPagerTitleView.setSelectedColor(ContextCompat.getColor(TimeActionActivity.this, R.color.action_select));
                timeActionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPage.setCurrentItem(index, true);
                    }
                });


                return timeActionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                TriangularPagerIndicator indicator = new TriangularPagerIndicator(context);
                indicator.setLineColor(Color.parseColor("#e94220"));
                return indicator;

            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPage);
    }

    private void dealWithViewPager() {
        ViewGroup.LayoutParams params = viewPage.getLayoutParams();
        params.height = ScreenUtil.getScreenHeightPx(this)  - magicIndicator.getHeight() + 1;
        viewPage.setLayoutParams(params);
    }

}
